package tcp;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * 
 * @author Kristopher L. Kalish, James Madison University
 * @version 1 - April 10, 2008
 *
 */
public class SessionHandler
{
	private Socket             socket;
	private ObjectOutputStream oos;
	
	public SessionHandler(Socket socket) throws IOException
	{
		this.socket = socket;
		oos = new ObjectOutputStream(socket.getOutputStream());
		System.out.println("Handler created for " + socket.getInetAddress()); //debug
	}
	
	public String getRemoteAddress()
	{
		return socket.getInetAddress().toString();
	}
	
	/**
	 * Sends an object over the socket.
	 * Note: the socket operations WILL block in 
	 * the caller's thread of execution
	 * 
	 * @param data Object to send
	 */
	public void send(Object data) throws IOException
	{
		System.out.println("Attemping to send: " + data);
		oos.writeObject(data);
		oos.flush();
		oos.reset();
		System.out.println("Sent.");
	}
	
	/**
	 * Cleans up connections. This includes closing the socket.
	 */
	public void cleanup() throws IOException
	{
		oos.close();
		socket.close();
	}
}
